package com.ruoyi.pingjiao.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.pingjiao.domain.Teacher;
import com.ruoyi.pingjiao.domain.query.AddEvaluationQueryParam;
import com.ruoyi.pingjiao.service.IEvalutionActivityService;
import com.ruoyi.pingjiao.service.IStudentService;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pingjiao.domain.Evaluation;
import com.ruoyi.pingjiao.service.IEvaluationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 评教管理Controller
 *
 * @author 胡浩鹏
 * @date 2022-10-22
 */
@Api("评教管理")
@RestController
@RequestMapping("/pingjiao/evaluation")
public class EvaluationController extends BaseController
{
    @Autowired
    private IEvaluationService evaluationService;
    @Autowired
    private IStudentService studentService;
    @Autowired
    private IEvalutionActivityService evalutionActivityService;

    /**
     * 查询评教管理列表
     */
    @PreAuthorize("@ss.hasPermi('pingjiao:evaluation:list')")
    @GetMapping("/list")
    public TableDataInfo list(Evaluation evaluation)
    {
        startPage();
        List<Evaluation> list = evaluationService.selectEvaluationList(evaluation);
        return getDataTable(list);
    }



    /**
     * 导出评教管理列表
     */
    @PreAuthorize("@ss.hasPermi('pingjiao:evaluation:export')")
    @Log(title = "评教管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Evaluation evaluation)
    {
        List<Evaluation> list = evaluationService.selectEvaluationList(evaluation);
        ExcelUtil<Evaluation> util = new ExcelUtil<Evaluation>(Evaluation.class);
        util.exportExcel(response, list, "评教管理数据");
    }

    /**
     * 获取评教管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('pingjiao:evaluation:query')")
    @GetMapping(value = "/{evaluationId}")
    public AjaxResult getInfo(@PathVariable("evaluationId") Long evaluationId)
    {
        return AjaxResult.success(evaluationService.selectEvaluationByEvaluationId(evaluationId));
    }

    /**
     * 新增评教管理
     */
    @PreAuthorize("@ss.hasPermi('pingjiao:evaluation:add')")
    @Log(title = "评教管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Evaluation evaluation)
    {
        return toAjax(evaluationService.insertEvaluation(evaluation));
    }

    /**
     * 新增评教
     */

    @PreAuthorize("@ss.hasPermi('pingjiao:evaluation:adds')")
    @Log(title = "评教管理", businessType = BusinessType.INSERT)
    @PostMapping("adds")
    public AjaxResult adds(@RequestBody Teacher teacher)
    {
        return toAjax(evaluationService.insertEvaluations(teacher));
    }


    /**
     * 修改评教管理
     */
    @PreAuthorize("@ss.hasPermi('pingjiao:evaluation:edit')")
    @Log(title = "评教管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Evaluation evaluation)
    {
        return toAjax(evaluationService.updateEvaluation(evaluation));
    }

    /**
     * 删除评教管理
     */
    @PreAuthorize("@ss.hasPermi('pingjiao:evaluation:remove')")
    @Log(title = "评教管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{evaluationIds}")
    public AjaxResult remove(@PathVariable Long[] evaluationIds)
    {
        return toAjax(evaluationService.deleteEvaluationByEvaluationIds(evaluationIds));
    }

    /**
     * 学生提交评教结果
     * @author hpf
     */
    @ApiOperation("学生提交评教结果")
    @PreAuthorize("@ss.hasPermi('pingjiao:evaluation:submit')")
    @Log(title = "评教管理", businessType = BusinessType.INSERT)
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody AddEvaluationQueryParam queryParam)
    {

        return toAjax(evaluationService.updateEvaluation(queryParam));
    }

    /**
     * 统计所有参评教师的评价结果（平均分）
     * @author hpf
     */
    @ApiOperation("统计所有参评教师的评价结果（平均分）")
    @PreAuthorize("@ss.hasPermi('pingjiao:evaluation:calculateEvaluationScoresOfTeacher')")
    @GetMapping("/calculateEvaluationScoresOfTeacher")
    public AjaxResult calculateEvaluationScoresOfTeacher( )
    {
        if(evalutionActivityService.calculateEvaluationScoresOfTeacher()){
            return AjaxResult.success("统计完成");
        }else {
            return AjaxResult.error("统计失败");
        }
    }

    //countEvaluationProgressByEvaluationActivityIdAndTeacherId

    /**
     * 查询某个评教教师评教进度
     */
    @PreAuthorize("@ss.hasPermi('pingjiao:evaluation:list')")
    @GetMapping("/countEvaluationProgressByEvaluationActivityIdAndTeacherId/{evalutionActivityId}/{teacherId}")
    public TableDataInfo countEvaluationProgressByEvaluationActivityIdAndTeacherId(@PathVariable Long teacherId, @PathVariable Long evalutionActivityId)
    {
        List<Map<String,Object>>  list =
                evaluationService.countEvaluationProgress(evalutionActivityId,teacherId);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('pingjiao:evaluation:list')")
    @GetMapping("/countEvaluationProgress/{teacherId}")
    public TableDataInfo countEvaluationProgress(@PathVariable Long teacherId)
    {
        List<Map<String,Object>>  list =
                evaluationService.countEvaluationProgressFromCourseclass(teacherId);
        return getDataTable(list);
    }
        @GetMapping("/getEvaluationId/{evaluationPaperId}")
    public AjaxResult getEvaluationIdByEvaluationPaperId(@PathVariable Long evaluationPaperId){
        return AjaxResult.success(evaluationService.getEvaluationIdByEvaluationPaperId(evaluationPaperId));
    }
}
